package net.togogo;

import net.togogo.bean.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentSpring {


    @Test
    public void init(){

        //1:初始化容器   读取配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //BeanFactory:ApplicationContext类型一个大的工厂
        //2：通过getBean()获取对象  指向对象在容器中注册id
        //2.1：getBean(String name)
        //Student student = (Student) context.getBean("student");
        //2.2：<T> T getBean(Class<T> requiredType) 不与id产生关联 但要求spring所管理的此类型对象必须唯一
        //Student student = context.getBean(Student.class);
        //2.3:<T> T getBean(String name, Class<T> requiredType)  双重参数指向同一个对象
        //从工厂中获取对象 通过注册的id在容器内部查找对象
        Student student = context.getBean("studentone", Student.class);
        System.out.println("student = " + student);
        student.sayHello();
    }

    //生命周期   读取配置文件时 对象就已经产生
    @Test
    public void createBean(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }

    @Test
    public void destoryBean(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        context.close();
    }
}
